<?php
class Zendvn_View_Helper_ZenjooUrl extends Zend_View_Helper_Url{
	
	public function zenjooUrl($urlOptions = array(), $route = null, $map = array(), $reset = false, $encode = true){
		
		$front = Zend_Controller_Front::getInstance();
        $request = $front->getRequest();
        $router = $front->getRouter();
        
	  	if (($urlOptions['action'])==null) {      
            $urlOptions['action'] =  $request->getActionName();
        }
        if (empty($urlOptions['controller'])){
             $urlOptions['controller'] = $request->getControllerName();
        }
        if (empty($urlOptions['module'])) {
            $urlOptions['module'] = $request->getModuleName();
        }
        if (empty($urlOptions['option'])) {
            $urlOptions['option'] = $request->getParam('option');
        }
        if (empty($urlOptions['control'])) {
            $urlOptions['control'] = $request->getParam('control');
        }
        if (empty($urlOptions['activity'])) {
            $urlOptions['module'] = $request->getParam('activity');
        }
        
       $params = $request->getParams();
       
       if(count($map)>0){
       		$mapArr = array();
       		foreach ($map as $key => $val){     
       			if(!empty($params[$val]))  {		
        			$mapArr[$val] = $params[$val];  
       			}     		
        	}
        	$urlOptions = array_merge($mapArr,$urlOptions);
        }
        //Nganguyen: Chưa kiểm tra trường hợp $mapArr là mảng rỗng => move lên trên
//        $urlOptions = array_merge($mapArr,$urlOptions);
        
        if(!$router->hasRoute($route) || $route === null){
        	$route = 'default';
        	$reset = true;      
			
        }
     
        return $this->url($urlOptions, $route, $reset,$encode);
	}
}